require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIWeaponSkin = {};
HelperFunc.SetUIEnv(UIWeaponSkin)
local this = UIWeaponSkin;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.weaponSkins = nil;
this.clicks = {};
this.hasInit = false;
this.weaponSpine = nil;

this.btnBackButton = nil;
this.objHeroPos = nil;
this.objContent = nil;
this.objSkinItem = nil;
this.drawWrappers = {};
this.objModel = nil;
this.tableSkinItem = {};

this.curHeroData = nil;
this.weaponConfig = nil;
this.weaponShowConfig = nil;
this.curWeaponData = nil;

function UIWeaponSkin.Init(data)
    --todo 应该小写 暂时先大写
    if weaponShowConfig == nil then
        weaponShowConfig = G.dataTable["t_weaponshow"];
    end
    curHeroData = logicMgr.HeroManager.getHeroDatabyPartnerId(data,true);
    local configId = 150000 + tonumber(curHeroData.partnerModelId);
    weaponConfig = G.dataTable["t_weapon"][tostring(configId)];

    local name, packageNAme = getUIName(PanelNames.UIWeaponSkin);
    panelMgr:ShowUI(name, packageNAme, this.onCreate, Config.useLocalUI);
end

function UIWeaponSkin.onCreate(obj)
    gameObject = obj;

    if not hasInit then
        hasInit = true;
        btnBackButton = obj:GetChild("BackButton");
        objHeroPos = obj:GetChild("HeroPos");
        objContent = obj:GetChild("lsit");
        --objSkinItem = obj:GetChild("Scroll View/SkinItem");
        txtTitleName = obj:GetChild("ItemName")
        this.addEvent();
    end
    this.refresh();
end

function UIWeaponSkin.addEvent()
    btnBackButton.onClick:Add(this.onClickClose);

end

function UIWeaponSkin.removeEvent()
    btnBackButton.onClick:Clear();
end

function UIWeaponSkin.LocalListenEvent()
    ListenEvent(Config.EventType.Update_WeaponAttrId, this.refresh);
end

function UIWeaponSkin.LocalCloseEvent()
    CloseEvent(Config.EventType.Update_WeaponAttrId, this.refresh);
end

function UIWeaponSkin.OnDestroy()
    this.removeEvent();
    destroy(weaponSpine)
    weaponSpine = nil
end

function UIWeaponSkin.OnEnable()
    this.LocalListenEvent();
end

function UIWeaponSkin.OnDisable()
    this.LocalCloseEvent();
end

function UIWeaponSkin.refresh()
    curWeaponData = logicMgr.EquipManager.tabWeapon[tostring(curHeroData.partnerId)];
    if curWeaponData.attiredId == 0 then
        curWeaponData.attiredId = decodeJsonStrToTabel(weaponConfig.f_WeaponShow)[1][2];
    end
    txtTitleName.text =getLanguage(weaponConfig.f_WeaponName);
    --local modelName = curHeroData.model;
    --[[if objModel == nil or objModel.name ~= modelName then
        if objModel ~= nil then
            destroy(objModel);
        end
        resMgr:LoadModelPrefab("Model/Role/" .. modelName, function(go)
            local drawWrapper = drawWrappers["weaponSpine"];
            if (drawWrapper == nil) then
                drawWrapper = GoWrapper.New();
                drawWrappers["weaponSpine"] = drawWrapper;
            end
            if (weaponSpine ~= nil) then
                destroyImmediate(weaponSpine);
            end
            weaponSpine = newObject(go);
            weaponSpine.name = "weaponSpine";
            weaponSpine:AddComponent(TCanvas);
            weaponSpine:AddComponent(TChangeSortingOrder)
            local aniObj=weaponSpine.transform:Find("Animation").gameObject;
            --local skinName = (G.dataTable["t_roleconfig"][modelName] or {}).skin;
            --if skinName ~= nil and skinName ~= '' then
            --    Util.SetSkin(aniObj,skinName);
            --end
            logicMgr.EquipManager.setWeaponSkin(aniObj, curHeroData.partnerModelId, true);

            drawWrapper:SetWrapTarget(weaponSpine, false)
            Util.Lua_SetLocalPositionXYZ(weaponSpine.transform,0,0,0)
            Util.Lua_SetLocalScaleXYZ(weaponSpine.transform,200,200,1);
            Util.Lua_SetLocalRotationXYZ(aniObj.transform,0,0,0)
            objHeroPos:SetNativeObject(drawWrapper);
        end);
    end--]]
    weaponSkins = decodeJsonStrToTabel(weaponConfig.f_WeaponShow);
    objContent.itemRenderer  = this.refreshItem;
    objContent.numItems  = #weaponSkins;
end

function UIWeaponSkin.refreshItem(idx, go)
    local data = weaponSkins[idx+1];
    local icon = go:GetChild("Icon");
    local usedGroup = go:GetController("use");
    local unlockGroup = go:GetController("rise");
    local unlockTip = go:GetChild("title");
    local haveCtrl = go:GetController("ishave")
    local showCofnig = weaponShowConfig[tostring(data[2])];
    if showCofnig ~= nil then
        icon.url = "ui://WeaponAtlas/" .. tostring(showCofnig.f_IconId);
    else
        icon.url = nil;
    end
    usedGroup.selectedIndex = data[2] == curWeaponData.attiredId and 1 or 0;
    unlockGroup.selectedIndex = curWeaponData.level < data[1] and 1 or 0;
    unlockTip.text = data[1];
    if tonumber(data[1]) <= 1 then
        haveCtrl.selectedIndex = 0
        unlockGroup.selectedIndex = 0
    else
        haveCtrl.selectedIndex = 1
    end
    if not clicks[idx] then
        clicks[idx] = true;
        go.onClick:Add(function()
            if data[2] ~= curWeaponData.attiredId and curWeaponData.level >= data[1] then
                weaponHandler.sendWeaponAttiredMessage(curWeaponData.partnerId, data[2]);
            end
        end);
    end
end

function UIWeaponSkin.onClickClose()
    closeUI(PanelNames.UIWeaponSkin);
end

return UIWeaponSkin;
